package com.lp.controller;


import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lp.service.DemoService;
import com.lp.wxpay.MyConfig;
import com.lp.wxpay.sdk.WXPay;

@Controller
public class WXPayController {
	@Autowired
	DemoService demoService;
	
	@RequestMapping(value="/wxPay")
	public void wxPay(HttpServletResponse response,Model model) throws Exception{
		
		MyConfig config = new MyConfig();
        WXPay wxpay = new WXPay(config);
        Random ra =new Random();
        int code = ra.nextInt(9);
        Map<String, String> data = new HashMap<String, String>();
        data.put("body", "腾讯充值中心-QQ会员充值");
        data.put("out_trade_no", "2018092010510000000"+code);
        data.put("device_info", "");
        data.put("fee_type", "CNY");
        data.put("total_fee", "1");
        data.put("spbill_create_ip", "123.12.12.123");
        data.put("notify_url", "http://www.example.com/wxpay/notify");
        data.put("trade_type", "NATIVE");  // 此处指定为扫码支付
        data.put("product_id", "12");
        PrintWriter printWriter = null;
        try {
            Map<String, String> resp = wxpay.unifiedOrder(data);
            /*
             *{result_code=SUCCESS, sign=023C259C9FBB142AD321ADB083ADB901CBBD67635820945382C23CA6FC35DEB3, 
             *mch_id=1234722402, prepay_id=wx20105807114886986ab49b0f2665893154, return_msg=OK, 
             *appid=wx1f01eabc3968e9d6, code_url=weixin://wxpay/bizpayurl?pr=lWud6X1, 
             *nonce_str=PNhQ1FZfjZY2drI9, return_code=SUCCESS, trade_type=NATIVE}
             */
            String html  = "";
            if(resp!=null&&resp.size()>0){
            	System.out.println(resp);
            	String code_url = resp.get("code_url");
            	model.addAttribute("code_url", code_url);
            	html  = "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'><title>付款二维码</title>"+
            			"<script type='text/javascript' src='http://cdn.staticfile.org/jquery/2.1.1/jquery.min.js'></script>"+
            			"<script type='text/javascript' src='http://cdn.staticfile.org/jquery.qrcode/1.0/jquery.qrcode.min.js'></script>"+
            			"</head><body><div id='qrcode'></div></body>"+
            			"<script>$(function () {$('#qrcode').qrcode({width:250,height:250,text:'"+code_url+"'});});</script></html>";
            }
            response.setContentType("text/html; charset=UTF-8");
            printWriter = response.getWriter();
            printWriter.write(html);
            printWriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
        	if (printWriter != null) {
				printWriter.close();
			}
		}
	}
	
}
